package com.tidc.seaserverweb.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tidc.seaservermodel.entity.Fres;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：冯涛滔
 * @date ：Created in 2020/9/11 19:31
 * @description：
 * @modified By：
 * @version:
 */
@Component
public class NotLoginHandler implements AuthenticationEntryPoint {
    @Autowired
    ObjectMapper objectMapper;
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        Map<String, Object> data = new HashMap<>();
        Fres<Map> fres = new Fres<>();
        fres.setData(data).getMeta().setStatus(422).setMessage("需要重新登入");
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(objectMapper.writeValueAsString(fres));
    }
}
